(function($) {
	var expandTree = function($tree) {
		$("li >ul", $tree).show();
		$("div.first_expandable", $tree).removeClass("first_expandable").addClass("first_collapsable");
		$("div.expandable", $tree).removeClass("expandable").addClass("collapsable");
		$("div.last_expandable", $tree).removeClass("last_expandable").addClass("last_collapsable");
		$("div.only_expandable", $tree).each(function(){
			var $this = $(this);
			if ($('li', $this.parent().next('ul')).length > 0) {
				$this.removeClass("only_expandable").addClass("only_collapsable");
			}
		});
	};
	$.extend($.fn, {
		baseTreeSearch : function(options){
			 var ops = $.extend({inputTextId : "#searchContent", alertMsg : "输入名称再查询", $p:null, count:0}, options);
			 return this.each(function(){
				 var $searchBtn = $(this), $page = ops.$p;
				 if ($page.length == 0) {
					 $page = DWZ.context($searchBtn);
				 }
				 var $searchInput = $(ops.inputTextId, $page);
				 $searchInput.on('keypress', function(event){
					 var e = event || window.event;
					 if (e.keyCode == 13) {
						 $searchBtn.click();
						 e.preventDefault();
					 }
				 });
				 $searchBtn.on('click', function(event){
					 var searchText = $searchInput.val();
					 if(!searchText.trim()){
						 alertMsg.warn(ops.alertMsg);
						 return false;
					 }
					 var $tree = $("ul.tree", $page);
					 expandTree($tree);
					 var $matchingItems = $("a:contains(" + searchText + ")", $tree), 
					 selectedIndex = -1;
					 $matchingItems.each(function(index, item){
						 var $item = $(item);
						 if ($item.parent().hasClass('selected')) {
							 selectedIndex = index;
						 }
					 });
					 $('div.selected', $tree).removeClass('selected');
					 if ($matchingItems.length > 0) {
						 var $targetItem = $($matchingItems[(selectedIndex + 1) % $matchingItems.length]);
						 $targetItem.parent().addClass('selected');
						 var $parent = $tree.parent(), scrollHeightMax = $parent[0].scrollHeight - $parent[0].offsetHeight;
						 // 针对树外面包裹了未限制高度的div
						 if (scrollHeightMax <= 0) {
							 $targetItem.focus();
							 $searchInput.focus();
							 return;
						 }
						 // 以下部分针对树的父div设置了layouth限制了高度的
						 var distance = $targetItem.offset().top - $parent.offset().top;
						 if (distance < 0) {
							 $parent.scrollTop(0);
							 distance = $targetItem.offset().top - $parent.offset().top;
						 }
						 if (distance > $parent[0].offsetHeight - 22) {
							 $parent.scrollTop($parent.scrollTop() + (distance + 22) * (scrollHeightMax / $tree.height()));
						 }
					 }
					 event.preventDefault();
				 });
			 });
		},
		
		_treeExpandable : function(options){
			var op = $.extend({$p:null}, options);
			return this.each(function(){
				$(this).click(function(){
					var $tree = $("ul.tree",op.$p);
					expandTree($tree);
				});
			});
		},
		
		_treeCollapsable : function(options){
			var op = $.extend({buttonId : "#collapsable", $p:null}, options);
			return this.each(function(){
				$(this).click(function(){
					var $tree = $("ul.tree",op.$p);
					$tree.find("li >ul").hide();
					$tree.find("div.first_collapsable").removeClass("first_collapsable").addClass("first_expandable");
					$tree.find("div.collapsable").removeClass("collapsable").addClass("expandable");
					$tree.find("div.last_collapsable").removeClass("last_collapsable").addClass("last_expandable");
				});
			});
		},
		_searchByValue:function(options){
			var op = $.extend({searchValue : null, $p:null}, options);
			return this.each(function(){
				setTimeout(function(){
	            	var $targetItem = $('a[tvalue=' + op.searchValue + ']:eq(0)', op.$p).click();
	            	$targetItem.parent().addClass('selected');
	            	var $tree=$targetItem.parents('.tree:first');
					var $parent = $tree.parent(), scrollHeightMax = $parent[0].scrollHeight - $parent[0].offsetHeight;
					 if (scrollHeightMax <= 0) {
						 $targetItem.focus();
						 $searchInput.focus();
						 return;
					 }
					var distance = $targetItem.offset().top - $parent.offset().top;
					if (distance < 0) {
						 $parent.scrollTop(0);
						 distance = $targetItem.offset().top - $parent.offset().top;
					}
					if (distance > $parent[0].offsetHeight - 22) {
						 $parent.scrollTop($parent.scrollTop() + (distance + 22) * (scrollHeightMax / $tree.height()));
					}
	            	}, 0)
			});
		}
	});
})(jQuery);